package serviceswarm

import (
	"context"
	"encoding/json"
	"github.com/aberic/gnomon"
	"github.com/docker/docker/api/types/swarm"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	swarmproto "gykjgit.dccnet.com.cn/chain/proto/swarm"
)

// ServiceLogs ServiceLogs
func ServiceLogs(url string, in *swarmproto.ReqServiceLogs) (*swarmproto.RespServiceLogs, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarmproto.NewServiceServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Logs(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarmproto.RespServiceLogs); ok {
			return response, nil
		}
		return nil, errors.New("service logs failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "service logs failed")
}

// ServiceList ServiceList
func ServiceList(url string) ([]swarm.Service, error) {
	var ss []swarm.Service
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarmproto.NewServiceServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.List(context.Background(), &swarmproto.ReqServiceList{})
	})
	if nil == err {
		if response, ok := resp.(*swarmproto.RespServiceList); ok {
			if err = json.Unmarshal(response.Services, &ss); nil == err {
				return ss, nil
			}
			return nil, errors.Wrap(err, "service list failed")
		}
		return nil, errors.New("service list failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "service list failed")
}

// ServiceInspect ServiceInspect
func ServiceInspect(url, serviceID string) (*swarm.Service, error) {
	var s swarm.Service
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarmproto.NewServiceServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Inspect(context.Background(), &swarmproto.ReqServiceInspect{ServiceID: serviceID})
	})
	if nil == err {
		if response, ok := resp.(*swarmproto.RespServiceInspect); ok {
			if err = json.Unmarshal(response.Service, &s); nil == err {
				return &s, nil
			}
			return nil, errors.Wrap(err, "service inspect failed")
		}
		return nil, errors.New("service inspect failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "service inspect failed")
}

// ServiceResource ServiceResource
func ServiceResource(url, serviceID string) (*swarmproto.Resources, error) {
	var r *swarmproto.Resources
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarmproto.NewServiceServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Resource(context.Background(), &swarmproto.ReqServiceResources{ServiceID: serviceID})
	})
	if nil == err {
		if response, ok := resp.(*swarmproto.RespServiceResources); ok {
			if err = json.Unmarshal(response.Resource, r); nil == err {
				return r, nil
			}
			return nil, errors.Wrap(err, "service resource failed")
		}
		return nil, errors.New("service resource failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "service resource failed")
}

// ServiceResourceAll ServiceResourceAll
func ServiceResourceAll(url string) (*swarmproto.Resources, error) {
	var r *swarmproto.Resources
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarmproto.NewServiceServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.ResourceAll(context.Background(), &swarmproto.ReqServiceResourcesAll{})
	})
	if nil == err {
		if response, ok := resp.(*swarmproto.RespServiceResourcesAll); ok {
			if err = json.Unmarshal(response.Resource, r); nil == err {
				return r, nil
			}
			return nil, errors.Wrap(err, "service resource all failed")
		}
		return nil, errors.New("service resource all failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "service resource all failed")
}
